package shoppingmall.reception.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import shoppingmall.reception.dto.PageBean;
import shoppingmall.reception.dto.ProductVO;
import shoppingmall.reception.dto.Result;
import shoppingmall.reception.service.ProductService;

/**
 * 前台商品部分的controller层
 */
@Slf4j
@RestController
public class ProductController {

    //ProductService service层实例
    @Autowired
    private ProductService productService;

    //分页查询所有的商品数据
    @GetMapping("/reception/product")
    public Result getProductList (@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        log.info("前台分页查询商品数据,page={},pageSize={}",page,pageSize);
        //调用service层方法查询商品数据
        PageBean productListPageBean = productService.getProductList(page, pageSize);
        if(productListPageBean.getTotal() == 0) {
            return Result.success("未查询到商品数据!");
        }
        return Result.success(productListPageBean);
    }

    //查询所有的商品数据
    @GetMapping("/reception/product/all")
    public Result getAllProductList () {
        log.info("前台查询所有商品数据");
        //调用service层方法查询商品数据
        PageBean productListPageBean = productService.getAllProductList();
        if(productListPageBean.getTotal() == 0) {
            return Result.success("未查询到商品数据!");
        }
        return Result.success(productListPageBean);
    }

    //根据对应的商品分类请求商品数据
    @GetMapping("/reception/product/classification")
    public Result getProductListByClassification (@RequestParam(defaultValue = "1") int classificationId) {
        log.info("查询对应分类的商品数据,classification={}",classificationId);
        //调用service层方法查询对应分类下的商品数据
        PageBean productListPageBean = productService.getProductListByClassification(classificationId);
        if(productListPageBean.getTotal() == 0) {
            return Result.success("未查询到商品数据!");
        }
        return Result.success(productListPageBean);
    }

    //根据商品id请求对应的商品数据
    @GetMapping("/reception/product/productId")
    public Result getProductInfoByProductId (@RequestParam int productId){
        log.info("查询某个商品数据,commodityId={}",productId);
        //调用service服务层对应的方法查询某个商品数据
        ProductVO productInfo = productService.getProductInfoByProductId(productId);
        if(productInfo != null){
            return Result .success(productInfo);
        }
        return Result.success("未查询到商品数据");
    }

    //根据商品名称模糊查询商品数据
    @GetMapping("reception/product/like")
    public Result getProductListByName (@RequestParam String productName) {
        //输出日志
        log.info("模糊查询商品列表信息,商品名称={}",productName);
        //调用service层方法
        PageBean pageBean = productService.getProductListByName(productName);
        //执行结果判断
        if(pageBean.getTotal() == 0) {
            return Result.error("未查询到商品数据!");
        }
        return Result.success(pageBean);
    }
}
